﻿using System.Linq;
using System.Text;
using KamLib;

namespace Ext.Net.UIS
{

    public class FieldUnion : Ext.Net.FieldContainer, ITermBox
    {
        public new Context Context { get; set; }
        public string Name { get; set; }
        public Term Term { get; set; }
        public TermNode ParentTermNode { get; set; }
        public TermNode TermNode { get; set; }

        public bool ControlsCreated { get; set; }
        public void CreateControls(UisCreateControlArgs e)
        {
            Layout = "HBoxLayout";

            this.UisGetTermNode();
            if (TermNode == null) return;

            TermNode.ApplyToContainer(this);

            if (FieldLabel.no())
            {
                if (TermNode.Title.yes() && (Term == null || TermNode.Title != Term.Name || TermNode.Count == 0))
                {
                    FieldLabel = TermNode.Title;
                }
                else
                {
                    var sb = new StringBuilder();

                    foreach (var node2 in TermNode)
                    {
                        if (node2.Title.no() || node2.IsLabel()) continue;
                        sb.Append(node2.Title);
                        sb.Append(", ");
                    }

                    if (sb.Length > 2)
                    {
                        sb.Length -= 2;
                        FieldLabel = sb.ToString();
                    }
                }
            }

            foreach (var node2 in TermNode)
            {
                node2.HidingTitle = true;
            }
        }
    }

}
